package club.hicode.daydayup.netty.udp.client;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * Udp的初始化
 *
 * @author Liu Chunfu
 * @date 2018-07-09 下午2:02
 **/
public class UdpChannelInitializer extends ChannelInitializer {

    private static final String DELIMITER = "$";

    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(" delimiter", new DelimiterBasedFrameDecoder(1024 * 8, Unpooled.copiedBuffer(DELIMITER.getBytes())));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());//如果需要发送结果必须在这个加入解码器
        pipeline.addLast("handler", new AgentHandler());
    }
}
